<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Service\FlyShopService;
use App\Service\MerchantJoinService;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 商家入驻升级相关
 * Class MerchantJoinController
 * @package App\Controller
 */
class MerchantJoinController extends AbstractController
{


    /**
     * @Inject
     * @var MerchantJoinService
     */
    private $merchantJoinServer;

    /**
     * 获取登录用户信息
     * @return int
     */
    private function get_user_id(): int
    {
        return $user_id = (int)$this->request->getHeaderLine('admin_id') ?? 0;
    }

    /**
     * 申请入驻
     * @return ResponseInterface
     */
    public function apply_join()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录先登录');
        }

        $params = $this->request->all();
        $params['apply_order_type'] = 1;
        $check_res = $this->merchantJoinServer->merchant_verify($params, $user_id);
        if (false === $check_res['res']) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, $check_res['msg'] ?? "操作失败");
        }

        $filtered_data = $check_res['data'];
        $filtered_data['user_id'] = $user_id;
        $res = $this->merchantJoinServer->apply_submit($filtered_data);
        if (!$res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '保存失败');
        }
        return $this->jsonResponse->success([], '申请成功');
    }

    /**
     * 验证是否处在申请状态中
     * @return ResponseInterface
     */
    public function check_join()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登陆');
        }
        return $this->jsonResponse->success(['join_state' => $this->merchantJoinServer->getApplyState($user_id, FlyShopService::STYLE_TYPE_SHOP)]);
    }

    /**
     * 验证美食入驻是否处在申请状态中
     * @return ResponseInterface
     */
    public function check_food_join()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登录');
        }
        $states = $this->merchantJoinServer->getApplyState($user_id, FlyShopService::STYLE_TYPE_FOOD);

        return $this->jsonResponse->success(['join_state' => $states['status']]);
    }

    /**
     * 获取当前美食入驻单状态
     * @return ResponseInterface
     */
    public function get_food_apply_info()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登陆');
        }
        // 获取用户当前入驻单
        return $this->jsonResponse->success($this->merchantJoinServer->getApplyNow($user_id, FlyShopService::STYLE_TYPE_FOOD));
    }

    /**
     * 获取当前入驻单状态
     * @return ResponseInterface
     */
    public function get_apply_info()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登陆');
        }
        // 获取用户当前入驻单
        return $this->jsonResponse->success($this->merchantJoinServer->getApplyNow($user_id, FlyShopService::STYLE_TYPE_SHOP));
    }

    /**
     * 完善入驻基本信息
     * @return ResponseInterface
     */
    public function finish_detail(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录先登录');
        }
        $params = $this->request->all();
        $params['apply_order_type'] = 2;
        $check_res = $this->merchantJoinServer->merchant_verify($params, $user_id);
        if (false === $check_res['res']) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, $check_res['msg'] ?? "操作失败");
        }
        $filtered_data = $check_res['data'];
        $filtered_data['user_id'] = $user_id;
        $res = $this->merchantJoinServer->apply_submit($filtered_data);
        if (!$res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '保存失败');
        }
        return $this->jsonResponse->success([], '申请成功');
    }

    /**
     * 其他类型入驻
     * @return ResponseInterface
     */
    public function otherJoinApply()
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录，请先登录');
        }
        $params = $this->request->all();
        $params['style_type'] = !in_array($params['style_type'], [FlyShopService::STYLE_TYPE_FOOD]) ? FlyShopService::STYLE_TYPE_FOOD : $params['style_type'];
        if (!in_array($params['submit_type'], [1, 2, 4])) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '参数错误');
        }
        if (!$this->merchantJoinServer->otherJoinApply($user_id, $params, $params['style_type'], $params['submit_type'] ?? 1)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '入驻申请失败');
        }
        return $this->jsonResponse->success([], '操作成功');
    }

    /**
     * 升级云商城
     * @return ResponseInterface
     */
    public function apply_upgrade(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录，请先登录');
        }
        $apply_res = $this->merchantJoinServer->merchant_upgrade($user_id);
        if (!$apply_res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '申请失败');
        }
        return $this->jsonResponse->success([], '申请成功');
    }

    /**
     * 修改信息申请
     * @return ResponseInterface
     */
    public function modify(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录，请先登录');
        }
        $params = $this->request->all();
        $params['apply_order_type'] = 3;
        // 验证修改新
        $v_res = $this->merchantJoinServer->merchant_verify($params, $user_id);
        if (!$v_res['res']) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '申请失败：' . $v_res['msg']);
        }
        // 保存店铺修改申请单
        $res = $this->merchantJoinServer->modifyShop($v_res['data']);
        if (!$res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '申请失败');
        }
        return $this->jsonResponse->success([], "申请成功");
    }

    /**
     * 入驻单修改申请单
     * @return ResponseInterface
     */
    public function apply_modify(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录，请先登录');
        }
        $params = $this->request->all();
        $params['apply_order_type'] = 4;
        $v_res = $this->merchantJoinServer->merchant_verify($params, $user_id);
        if (!$v_res['res']) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '申请失败:' . $v_res['msg']);
        }
        $filtered_data = $v_res['data'];
        $filtered_data['user_id'] = $user_id;
        // 保存入驻修改申请单
        $res = $this->merchantJoinServer->apply_submit($filtered_data);
        if (!$res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '申请失败');
        }
        return $this->jsonResponse->success([], '申请成功');
    }

    /**
     * 获取已填写的店铺基本信息
     * @return ResponseInterface
     */
    public function getMyShopBaseInfo(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        return $this->jsonResponse->success($this->merchantJoinServer->getMyShop($user_id), 'success!');
    }

}